package com.outsourcing.bottle.util;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.format.Formatter;

import com.outsourcing.bottle.remoteimage.ImageCache;
/**
 * 
 * @author 06peng
 *
 */
public class BottleApplication extends Application {
	
	public static BottleApplication mDemoApp;
	private ImageCache mImageCache;
	
	public static BottleApplication getInstance() {
		return mDemoApp;
	}

	
    private ThumbnailCache mLruImageCache;
	
	public ThumbnailCache getLruImageCache() {
		return mLruImageCache;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		mDemoApp = this;
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		mImageCache = new ImageCache();
	
		ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
		mLruImageCache = new ThumbnailCache(memoryClassBytes / 10);

	}
	

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	public ImageCache getImageCache() {
		return mImageCache;
	}
	
	   /**
     * Simple extension that uses {@link Bitmap} instances as keys, using their
     * memory footprint in bytes for sizing.
     */
    public static class ThumbnailCache extends LruCache<String, Bitmap> {
        public ThumbnailCache(int maxSizeBytes) {
            super(maxSizeBytes);
        }
        
//        @Override
//        protected int sizeOf(String key, Bitmap value) {
//            return value.getByteCount();
//        }
    }
	
	
	
}
